package com.search.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.base.core.lang.StringUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.search.dao.repo.ItemRepository;
import com.search.entity.Item;
import com.search.service.ItemQueueSink;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;

@Service
@EnableBinding(ItemQueueSink.class)
public class IngestItemService {

    @Autowired
    private ItemRepository productRepository;

    private static final String DELETE = "bootstrap-delete";
    private static final String INSERT = "bootstrap-insert";
    private static final String UPDATE = "bootstrap-update";

    @StreamListener(ItemQueueSink.INPUT)
    public void handle(String content) {
        ObjectMapper om = new ObjectMapper();
        // 解析binlog操作
        String operation = null;
        // 将Json解析成JsonNode
        JsonNode jsonNode = null;

        try {
            operation = om.readTree(content).path("type").asText();
            jsonNode = om.readTree(content).path("data");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        if (ObjectUtil.isNotEmpty(jsonNode)) {
            Item product = new Item();
            String name = ObjectUtil.isNotNull(jsonNode.get("name")) ? jsonNode.get("name").asText(): "";
            String drugsNo = ObjectUtil.isNotNull(jsonNode.get("drugs_no")) ? jsonNode.get("drugs_no").asText(): "";
            Long categoryId = ObjectUtil.isNotNull(jsonNode.get("category_id")) ? jsonNode.get("category_id").asLong(): 0;
            Long id = ObjectUtil.isNotNull(jsonNode.get("id")) ? jsonNode.get("id").asLong(): 0;
            String expireTime = ObjectUtil.isNotNull(jsonNode.get("expire_time")) ? jsonNode.get("expire_time").asText(): "";
            String produceTime = ObjectUtil.isNotNull(jsonNode.get("produce_time")) ? jsonNode.get("produce_time").asText(): "";
            BigDecimal quantity = ObjectUtil.isNotNull(jsonNode.get("quantity"))
                    ? new BigDecimal(jsonNode.get("quantity").asDouble()) : new BigDecimal(0);
            String specs = ObjectUtil.isNotNull(jsonNode.get("specs")) ? jsonNode.get("specs").asText(): "";
            Long storeId = ObjectUtil.isNotNull(jsonNode.get("store_id")) ? jsonNode.get("store_id").asLong(): 0;
            BigDecimal salePrice = ObjectUtil.isNotNull(jsonNode.get("quantity"))
                    ? new BigDecimal(jsonNode.get("sale_price").asDouble()) : new BigDecimal(0);
            product.setName(name);
            product.setCategoryId(categoryId);
            product.setDrugsNo(drugsNo);
            product.setQuantity(quantity);
            product.setSpecs(specs);
            product.setSalePrice(salePrice);
            product.setId(id);
            product.setStoreId(storeId);
            String parsePattern = "yyyy-MM-dd HH:mm:ss";
            try {
                if(StringUtils.isNotEmpty(expireTime)){
                    product.setExpireTime(DateUtils.parseDate(expireTime, parsePattern));
                }
                if(StringUtils.isNotEmpty(produceTime)){
                    product.setProduceTime(DateUtils.parseDate(produceTime, parsePattern));
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

            switch (operation) {
                case INSERT:
                case UPDATE:
                    productRepository.save(product);
                    break;
                case DELETE:
                    productRepository.deleteById(product.getId());
                    break;
                default:
                    break;
            }
        }
    }
}
